
package com.thinkgem.jeesite.common.component.redis.element;

import java.util.List;
import java.util.Set;

import redis.clients.jedis.Jedis;

/** 
 * 放置rds的容器类(set)元素操作
 *
 */
public class RdsSets {

	public RdsSets(Jedis jedis) {
	    super();
	    this.jedis = jedis;
    }

	public Jedis getJedis() {
    	return jedis;
    }

	public void setJedis(Jedis jedis) {
    	this.jedis = jedis;
    }

	/**
	 * set结构增加复数个members到key中.
	 * (如果members不存在则添加成功1,否则失败0)
	 * @param key
	 * @param members
	 * @return
	 * 2014-10-28
	 */
	public Long sadd(String key,String... members) {
		return getJedis().sadd(key, members);
	}
	
	/**
	 * set结构删除复数个members从key中,与sadd对应.
	 * (如果删除失败不计数,最后返回删除成功的个数)
	 * @param key
	 * @param members
	 * @return
	 * 2014-10-28
	 */
	public Long srem(String key,String... members) {
		return getJedis().srem(key, members);
	}

	/**
	 * set结构随机弹出key中的一个member
	 * @param key
	 * @return
	 * 2014-10-28
	 */
	public String spop(String key) {
		return getJedis().spop(key);
	}

	/**
	 * set结构从源key中将指定的member移动到目标key中.
	 * (源key中将去掉该member,如果目标key中已存在该member,则直接覆盖)
	 * @param srckey
	 * @param dstkey
	 * @param member
	 * @return
	 * 2014-10-28
	 */
	public Long smove(String srckey,String dstkey,String member) {
		return getJedis().smove(srckey, dstkey, member);
	}

	/**
	 * set结构统计某个key中的member数量
	 * @param key
	 * @return
	 * 2014-10-28
	 */
	public Long scard(String key) {
		return getJedis().scard(key);
	}

	/**
	 * set结构判断当前key中是否已包含member,有则返回true,没有则返回false
	 * @param key
	 * @param member
	 * @return
	 * 2014-10-28
	 */
	public boolean sismember(String key,String member) {
		return getJedis().sismember(key, member);
	}

	/**
	 * set结构返回key1..keyN之间member的交集
	 * @param keys
	 * @return
	 * 2014-10-28
	 */
	public Set<String> sinter(String... keys) {
		return getJedis().sinter(keys);
	}

	/**
	 * set结构将key1...keyN之间member的交集存入目标key中,并返回操作状态码
	 * @param dstkey
	 * @param keys
	 * @return
	 * 2014-10-28
	 */
	public Long sinterstore(String dstkey,String... keys) {
		return getJedis().sinterstore(dstkey, keys);
	}

	/**
	 * set结构返回key1..keyN之间member的并集
	 * @param keys
	 * @return
	 * 2014-10-28
	 */
	public Set<String> sunion(String... keys) {
		return getJedis().sunion(keys);
	}

	/**
	 * set结构将key1...keyN之间member的并集存入目标key中,并返回操作状态码
	 * @param dstkey
	 * @param keys
	 * @return
	 * 2014-10-28
	 */
	public Long sunionstore(String dstkey,String... keys) {
		return getJedis().sunionstore(dstkey, keys);
	}

	/**
	 * set结构返回key1与key2...keyN并集之间的差集
	 * (key1-(key1&(key2|...|keyN)))
	 * @param keys
	 * @return
	 * 2014-10-28
	 */
	public Set<String> sdiff(String... keys) {
		return getJedis().sdiff(keys);
	}

	/**
	 * set结构将key1...keyN之间member的差集存入目标key中,并返回操作状态码
	 * (差集求法同sdiff)
	 * @param dstkey
	 * @param keys
	 * @return
	 * 2014-10-28
	 */
	public Long sdiffstore(String dstkey,String... keys) {
		return getJedis().sdiffstore(dstkey, keys);
	}

	/**
	 * set结构返回key中对应的所有members
	 * @param key
	 * @return
	 * 2014-10-28
	 */
	public Set<String> smembers(String key) {
		return getJedis().smembers(key);
	}

	/**
	 * set结构随机返回key中的某个member
	 * @param key
	 * @return
	 * 2014-10-28
	 */
	public String srandmember(String key) {
		return getJedis().srandmember(key);
	}

	/**
	 * set结构随机返回key中一定数量的member
	 * (2.6.0新接口,老版本服务端不一定支持)
	 * @param key
	 * @param count
	 * @return
	 * 2014-10-28
	 */
	public List<String> srandmember(String key,int count) {
		return getJedis().srandmember(key, count);
	}

	private Jedis jedis;//当前客户端
}
